// 引入SDK核心类
var QQMapWX = require('../../resources/map/qqmap-wx-jssdk.js');
var qqmapsdk;
//获取应用实例
var app = getApp()
Page({
  data: {
    proData: [], //医生数据
    hlist: [],  //医院数据
    dlist: [],  //医生分类
    htype: [],  //医院分类
    distance: 0,
    docCatId: 0,//医生分类
    hosCatId: 0,//医院分类
    tabTxt: ['按医生专科', '按医院等级'],
    //tab文案
    tab: [true, true],
    page: 2,//当前页码
    yi:true,
    yuan: false,
  },
  onReady: function () {
    //初始化数据
    // var self = this;
    // self.getFilter();
  },

  // 医院点击事件
  hospital:function(e){
    var hosId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../hospital/hospital?hosId=' + hosId,
      })
  },

  // 医生点击事件
  bindViewTap: function (e) {
    var docId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?docId=' + docId,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 选项卡
  filterTab: function (e) {
    console.log(e)
    var data = [true, true], 
    index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    if (index == 0) {
      this.setData({
        yi: true,
        yuan: false,
      })
    }; 
    if (index == 1) {
      this.setData({
        yuan: true,
        yi: false,
      })
    };
    this.setData({
      tab: data
    })
  },

  //筛选项点击操作
  filter: function (e) {
    var self = this,
      id = e.currentTarget.dataset.id,
      txt = e.currentTarget.dataset.txt,
      tabTxt = this.data.tabTxt;
    switch (e.currentTarget.dataset.index) {
      case '0':
        tabTxt[0] = txt;
        self.setData({
          page: 1,
          tab: [true, true],
          tabTxt: tabTxt,
          docCatId: id,
        });
        self.getDocData();
        break;
      case '1':
        tabTxt[1] = txt;
        self.setData({
          page: 1,
          tab: [true, true],
          tabTxt: tabTxt,
          hosCatId: id,
        });
        self.getHosData();
        break;
    }
    //数据筛选
    //self.getData();
  },

  // 页面加载
  onLoad: function (option) {
    console.log(option);
    if (option.titles==1){
      this.setData({
        yi: false,
        yuan: true,
      })
    }
  },
  // 页面显示
  onShow: function () {
    // 调用接口
    //this.getData();
    //this.gei();
    // this.demo();
    var that = this;
    wx.request({
      url: app.api.hostUrl + '/Api/Index/getdata',
      method: 'post',
      data: {
        uid: app.api.userId
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var pro = res.data.pro;
        var hlist = res.data.hlist;
        var dlist = res.data.dlist;
        var htype = res.data.htype;
        that.setData({
          proData: pro,
          hlist: hlist,
          dlist: dlist,
          htype: htype,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },
  // 我的坐标距离计算
  gei: function () {
    var that = this;
    wx.getLocation({
      success: function (res) {
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        qqmapsdk.calculateDistance({
          mode: 'walking',//步行，驾车为'driving'
          from: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          // 地方坐标
          to: [
            {
              latitude: 23.171847,
              longitude: 113.318052
            }, {
              latitude: 23.171877,
              longitude: 113.318092
            },
            {
              latitude: 23.171887,
              longitude: 113.318095
            },
            {
              latitude: 23.171887,
              longitude: 113.318095
            },
            {
              latitude: 23.171887,
              longitude: 113.318095
            },
          ],
          success: function (res) {
            var resData = []
            console.log(res);
            console.log(res.result.elements);
            var p = res.result.elements;
            console.log(p.length);
            for (var i = 0; i < p.length; i++) {
              var a = p[i].distance;
              console.log(a)
              resData.push({
                distance: a,
                id: 1,
                name: '广东人民第一医院',
                png1: '★',
                png2: '★',
                png3: '★',
                png4: '★',
                png5: '★',
                briefAddr: '三甲医院',
              });
              that.setData({
                list: resData
              })
            }
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          },
        });
      },
    })
  },

  //获取数据
  getData: function () {
    var that = this;
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: '3F4BZ-6RBR6-Q3HSQ-ML5JH-KF22H-YTBRQ'
    });
  },

  //按分科加载医生数据
  getDocData: function () {
    var that = this;
    wx.request({
      url: app.api.hostUrl + '/Api/Doctor/get_more',
      method: 'post',
      data: {
        uid: app.api.userId,
        cat_id: that.data.docCatId,
        page: 1,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var pro = res.data.pro;
        if (pro==''){
          wx.showToast({
            title: '没有找到相关专科医生！',
            duration: 2000
          });
        }
        that.setData({
          proData: pro,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  //按分科加载医生数据
  getHosData: function () {
    var that = this;
    wx.request({
      url: app.api.hostUrl + '/Api/Shangchang/getlists',
      method: 'post',
      data: {
        uid: app.api.userId,
        cid: that.data.hosCatId,
        page: 1,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var hlist = res.data.hlist;
        if (hlist == '') {
          wx.showToast({
            title: '没有找到更多数据！',
            duration: 2000
          });
        }
        that.setData({
          hlist: hlist,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  // 数据更新
  // doRefresh: function () {
  //   wx.showLoading({ title: "数据更新中!" });
  //   this.gei();
  // },
})
